﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using Coolite.Ext.Web;
using GUESTSMANAGEMENT.Controller;
namespace GUESTSMANAGEMENT.View.Event
{
    public partial class frmRelationship : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        
        }

        protected void Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            try
            {
                this.userStore.DataSource = Controller.User.getGridListRelationship();
                this.userStore.DataBind();
            }
            catch
            {
                Ext.Msg.Alert("Alerta!", "Ocorreu um erro durante a transação! Tente novamente!").Show();
            }
        }

        public Store GridStore
        {
            get;
            set;
        }

        protected void SubmitSelection(object sender, AjaxEventArgs e)
        {
            try
            {
                string json = e.ExtraParams["Values"];

                if (string.IsNullOrEmpty(json))
                {
                    return;
                }
                string idEvent = this.idEvent.Text;
                int idEvento = int.Parse(idEvent);
                List<gmuser> users = JSON.Deserialize<List<gmuser>>(json);
                foreach (gmuser user in users)
                {
                    using (guestsmanagementEntities context = new guestsmanagementEntities())
                    {
                        gmeventuser eventUserCompare = Controller.EventUser.getByEventUserId((int)user.CDUSER, idEvento);
                        if (eventUserCompare == null)
                        {
                            gmevent objEvent = null;
                            gmuser objUser = null;
                            gmeventuser eventuser = new gmeventuser();
                            objUser = (from usua in context.gmuser where usua.CDUSER == user.CDUSER select usua).FirstOrDefault();
                            objEvent = (from evento in context.gmevent where evento.CDEVENT == idEvento select evento).FirstOrDefault();
                            eventuser.QTBF = objEvent.QTBF;
                            eventuser.QTBM = objEvent.QTBM;
                            eventuser.QTFF = objEvent.QTFF;
                            eventuser.QTFM = objEvent.QTFM;
                            eventuser.gmevent = objEvent;
                            eventuser.gmuser = objUser;
                            context.AddTogmeventuser(eventuser);
                            context.SaveChanges();
                            Ext.Msg.Alert("Alerta!", "Relação de promoters realizada com sucesso!").Show();
                        }
                    }
                }

                Dictionary<string, string>[] users1 = JSON.Deserialize<Dictionary<string, string>[]>(json);
                foreach (Dictionary<string, string> row in users1)
                {
                    string name = row["NMUSER"];
                    string id = row["CDUSER"];
                }
            }
            catch
            {
                Ext.Msg.Alert("Alerta!", "Ocorreu um erro durante a transação! Tente novamente!").Show();
            }
        }

        public void ShowGridRel()
        {
            this.winRel.Show();
        }

        public void SetGridRel(gmevent evento)
        {
            try
            {
                using (guestsmanagementEntities context = new guestsmanagementEntities())
                {
                    this.userStore.DataSource = Controller.User.getGridListRelationship();
                    this.userStore.DataBind();

                    this.idEvent.Text = evento.CDEVENT.ToString();
                }
            }
            catch
            {
                Ext.Msg.Alert("Alerta!", "Ocorreu um erro durante a transação! Tente novamente!").Show();
            }

        }

        public class User
        {
            public User(string name, int id)
            {
                this.Name = name;
                this.Id = id;
            }
            public User()
            {
            }

            public string Name { get; set; }
            public int Id { get; set; }
        }

    }
}